home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 296_01.zip / LITTLE.Y < prev    next >
Text File  |  1993-04-01  |  4KB  |  191 lines

  1. /* This is a short version of the C Grammar, it includes mainly */
  2. /* the productions that had semantic action code attached for the */
  3. /* migrator prototype. The full 400 line grammar is supplied with */
  4. /* the source code for the article by the C Users Journal   */
  5.  
  6. %token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
  7. %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
  8. %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  9. %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  10. %token XOR_ASSIGN OR_ASSIGN TYPE_NAME
  11.  
  12. %token TYPEDEF EXTERN STATIC AUTO REGISTER
  13. %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
  14. %token STRUCT UNION ENUM ELIPSIS RANGE
  15.  
  16. %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
  17.  
  18. %start program
  19. %%
  20.  
  21. primary_expr
  22.     : identifier
  23.     | CONSTANT
  24.     | STRING_LITERAL
  25.     | '(' expr ')'
  26.     ;
  27.  
  28. declaration
  29.     : declaration_specifiers ';'
  30.     | declaration_specifiers init_declarator_list ';'
  31.     | TYPEDEF { in_tdef = TRUE; su = FALSE; } tdef ';' { in_tdef = FALSE; }
  32.     ;
  33.  
  34. tdef
  35.     : type_specifier_list opt_decl
  36.     | declarator
  37.     ;
  38.  
  39. type_specifier
  40.     : CHAR
  41.     | SHORT
  42.     | INT
  43.     | LONG
  44.     | SIGNED
  45.     | UNSIGNED
  46.     | FLOAT
  47.     | DOUBLE
  48.     | CONST
  49.     | VOLATILE
  50.     | VOID
  51.     | struct_or_union_specifier  { su = ( su > 0 ? --su : 0); }
  52.     | enum_specifier  { enumflag = FALSE; }
  53.     | TYPE_NAME
  54.     ;
  55.  
  56. struct_or_union_specifier
  57.     : struct_or_union identifier '{' struct_declaration_list '}'
  58.     | struct_or_union '{' struct_declaration_list '}'
  59.     | struct_or_union identifier
  60.     ;
  61.  
  62. struct_or_union
  63.     : STRUCT
  64.         { su += 1; }
  65.     | UNION
  66.         { su += 1; }
  67.     ;
  68.  
  69. enum_specifier
  70.     : enum '{' enumerator_list '}'
  71.     | enum identifier '{' enumerator_list '}'
  72.     | enum identifier
  73.     ;
  74.  
  75. enum
  76.     : ENUM { enumflag = TRUE; }
  77.     ;
  78.  
  79. enumerator_list
  80.     : enumerator
  81.     | enumerator_list ',' enumerator
  82.     ;
  83.  
  84. enumerator
  85.     : identifier
  86.     | identifier '=' constant_expr
  87.     ;
  88.  
  89. declarator
  90.     : declarator2
  91.     | pointer declarator2
  92.     ;
  93.  
  94. declarator2
  95.     : identifier
  96.     | '(' declarator ')'
  97.     | declarator2 '[' ']'
  98.     | declarator2 '[' constant_expr ']'
  99.     | declarator2 '(' ')' 
  100.         {
  101.         func_decl = TRUE;
  102.         if ( edit_mode )
  103.             {
  104.             write_proto(NOARGS);
  105.             ed_delete();
  106.             }
  107.         }
  108.     | declarator2 '(' parameter_type_list ')'
  109.     | declarator2 '(' parameter_identifier_list
  110.         {
  111.         if ( edit_mode )
  112.             {
  113.             write_proto(WITH_ARGS);
  114.             ed_delete();
  115.             }
  116.         }  ')' 
  117.     ;
  118.  
  119. left_curly
  120.     : '{' {proto_flag=FALSE;}
  121.     ;
  122.  
  123. declaration_list
  124.     : declaration
  125.     | declaration_list declaration
  126.     ;
  127.  
  128. statement_list
  129.     : statement
  130.     | statement_list statement
  131.     ;
  132.  
  133. expression_statement
  134.     : ';'
  135.     | expr ';'
  136.     ;
  137.  
  138. program
  139.     : file { ed_flush(); }
  140.     ;
  141.  
  142. file
  143.     : external_definition
  144.     | file external_definition
  145.     ;
  146.  
  147. external_definition
  148.     : function_definition
  149.     | declaration 
  150.         {
  151.         if ( func_decl == TRUE )
  152.         {
  153.         if ( edit_mode )
  154.             ed_delete();
  155.         func_decl = FALSE;
  156.         }
  157.         };
  158.  
  159. function_definition
  160.     : declarator function_body
  161.     | declaration_specifiers declarator function_body
  162.     ;
  163.  
  164. function_body
  165.     : compound_statement
  166.     | declaration_list compound_statement
  167.     ;
  168.  
  169. identifier
  170.     : IDENTIFIER
  171.         {
  172.         if ( in_tdef && su == 0 && !enumflag)
  173.         storesym(yytext,0L);  /* save only typedef names */
  174.         }
  175.     ;
  176. %%
  177.  
  178. #include <stdio.h>
  179. #include "ctocxx.h"
  180.  
  181. extern char *yytext;
  182. extern int proto_flag;
  183. extern int lineno;
  184. int in_tdef = FALSE;    /* flag used to control storing typedef idents in table */
  185. int su = FALSE;        /* TRUE if just after a struct/union keyword */
  186. int enumflag = FALSE;    /* TRUE if in an enum specifier */
  187. int func_decl = FALSE;
  188. int edit_mode = FALSE;  /* TRUE only when in the original source file, not includes */
  189. extern FILE *efd;
  190.  
  191.